package com.mmall.seckill.common.config;


import com.mmall.seckill.common.Interceptor.AccessInterceptor;
import com.mmall.seckill.common.filter.SessionExpireFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by megan on 2018/6/18.
 * 自动注入用户信息
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Resource
    private UserArgumentResolver userArgumentResolver;
    @Resource
    private AccessInterceptor accessInterceptor;

    /** 通过参数拿用户信息 **/
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(userArgumentResolver);
    }

    /** 拦截器 **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注入拦截器
        registry.addInterceptor(accessInterceptor);
    }

    /** 配置过滤器 **/
    @Bean
    public FilterRegistrationBean httpFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new SessionExpireFilter());
        /** 配置过滤器要拦截的路径 **/
        filterRegistrationBean.addUrlPatterns("/**");
        return filterRegistrationBean;
    }


}
